package cn.com.jslh.boarpro.util.template;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PointF;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.dlazaro66.qrcodereaderview.QRCodeReaderView;

import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.view.fragment.AddTemplateFragment;
import cn.com.jslh.boarpro.main.view.fragment.ShowPigInfoFragment;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.ScanDialog;
import cn.com.jslh.boarpro.util.WebService;

/**
 * Created by Administrator on 2017/11/17.
 */

public class TxtScanTempalte extends AddTempBaseItem implements AdapterInterface {
    @Override
    public String getFlag() {
        return "txtScan";
    }

    @Override
    public int getLayout() {
        return R.layout.add_template_txtscan_item_layout;
    }

    @Override
    public SlimInjector<? extends AddTempBaseItem> getSlimInjetor() {
        SlimInjector<TxtScanTempalte> inj = new SlimInjector<TxtScanTempalte>() {
            @Override
            public void onInject(final TxtScanTempalte data, IViewInjector injector) {
                JSONObject config = new JSONObject();
                try {
                    config = new JSONObject(data.getConfig());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                final JSONObject finalConfig = config;

                injector.text(R.id.titleText, data.getTitle())
                        .with(R.id.valueText, new IViewInjector.Action() {
                            @Override
                            public void action(final View view) {
                                TextView textView = (TextView) view;
                                if (data.getShowInfo() == null || data.getShowInfo().equals("")) {
                                    textView.setText(data.getPlaceholder());
                                } else {
                                    textView.setText(data.getShowInfo());
                                }
                                JSONUtil.configValueTextView(textView, finalConfig);
                            }
                        })
                        .with(R.id.titleText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                JSONUtil.configTextView((TextView) view, finalConfig);
                            }
                        })
                        .clicked(R.id.valueText, new View.OnClickListener() {
                            @Override
                            public void onClick(final View v) {
                                final EditText editText = new EditText(v.getContext());
                                editText.setWidth(100);
                                editText.setHint(data.getShowInfo());
                                editText.setGravity(Gravity.CENTER);

                                new AlertDialog.Builder(v.getContext())
                                        .setView(editText)
                                        .setTitle(data.getPlaceholder())
                                        .setCancelable(true)
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                try {
                                                    if(editText.getText().toString().length()>1) {
                                                        inspectData(v.getContext(), finalConfig.getString("checkAction"), editText.getText().toString(), data.getKey(), data);
                                                    }else {
                                                        Toast.makeText(v.getContext(), "请输入正确的内容", Toast.LENGTH_SHORT).show();
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }).setNegativeButton("取消", null)
                                        .show();
                            }
                        })
                        .clicked(R.id.scanner_view, new View.OnClickListener() {
                            @Override
                            public void onClick(final View v) {
                                new ScanDialog.Builder(v.getContext())
                                        .setQrCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                                            @Override
                                            public void onQRCodeRead(final String text, PointF[] points) {
                                                try {
                                                    if(text.length()>1) {
                                                        inspectData(v.getContext(), finalConfig.getString("checkAction"), text, data.getKey(), data);                                                    }else {
                                                    }
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }).create().show();
                            }
                        });
            }
        };
        return inj;
    }

    private void inspectData(final Context context, String checkAction, final String text, String keyid, final TxtScanTempalte data) {
        new TempNet.Builder(context)
                .addParam("content", text)
                .setActionName(checkAction)
                .setKeyId(keyid)
                .setNetLitener(new WebService.NetSuccessListener() {
                    @Override
                    public void Success(String s) throws JSONException {
                        JSONObject jsonObject = new JSONObject(s);
                        if (jsonObject.getString("flag").equals("1")) {
                            ShowPigInfoFragment.postEvent(jsonObject.getJSONArray("data"));
                            data.setShowInfo(text);
                            data.setResult(jsonObject.getString("id"));
                            TemplateHelper.updataItems(context, data);
                            AddTemplateFragment.notifyDatas();
                        } else {
                            Toast.makeText(context, jsonObject.getString("data"), Toast.LENGTH_SHORT).show();
                        }
                    }
                }).build().doAction();
    }
}
